import db from '../../utils/dbconf';
import { JSON_parse } from '../../utils/functions';
import makeres from '../../utils/res';
import { TargetDataNoneError } from '../../utils/classModal';

import { TABLE_NAME as RELACTION_TABLE } from './relactionTool';
export const TABLE_NAME = 'MJ_CARD';

const SHOULD_PARSE_KEY = ['deck', 'partition', 'linner'];

// select用
async function getByName(name, u_id) {
	const data = await db
		.table(TABLE_NAME)
		.where({
			name: ['LIKE', `%${name}%`],
			_complex: {
				u_id,
				public: 1,
				_logic: 'OR',
			},
		})
		.limit(20)
		.select();
	return data;
}

export const cardcrud = {
	getdetail: async ({ params: { id } }, res) => {
		const { u_id } = res.locals;
		const item = await db.table(TABLE_NAME).where({ id }).find();
		JSON_parse.decode(SHOULD_PARSE_KEY, item);
		return res.send(makeres.success(item));
	},
	getlist: async ({ query }, res) => {
		const { u_id } = res.locals;
		let { page = 1, size = 10, name = '', group_id = 0, commond } = query;
		if (commond == 'getbyname') {
			const list = await getByName(name, u_id);
			return res.send(makeres.success({ list }));
		}
		let card_ids = ['EXP', 'IS NOT NULL'];
		let list;
		let total;
		if (Number(group_id)) {
			list = await db
				.table(RELACTION_TABLE)
				.alias('relac')
				.field('cd.*')
				.join({
					table: TABLE_NAME,
					as: 'cd',
					join: 'left',
					on: ['card_id', 'id'],
				})
				.where({ group_id })
				.order('weight')
				.select();
			// if (!cards || !cards.length) return res.send(makeres.success({ list: [] }));
			// card_ids = ['IN', cards.map(({ card_id }) => card_id)];
		} else {
			const { count, data } = await db
				.table(TABLE_NAME)
				.field('partition,linner', true)
				.where({
					name: ['LIKE', `%${name}%`],
					id: card_ids,
					_complex: {
						u_id,
						public: 1,
						_logic: 'OR',
					},
				})
				.page(page, size)
				.countSelect();
			list = data;
			total = count;
		}
		list.length &&
			list.map((e) => {
				e.deck = JSON.parse(e.deck);
				delete e.partition;
				delete e.linner;
			});
		return res.send(makeres.success({ total, list }));
	},
	add: async ({ body: data }, res) => {
		const { u_id } = res.locals;
		data = JSON_parse.encode(SHOULD_PARSE_KEY, data);
		let id = data.id;
		if (!id) {
			id = await db.table(TABLE_NAME).add({ ...data, u_id });
			return res.send(makeres.success({ id }));
		} else {
			const ok = await db.table(TABLE_NAME).where({ id, u_id }).update(data);
			if (!ok) throw new TargetDataNoneError();
			return res.send(makeres.putsuccess());
		}
	},
	change: async ({}, res) => {
		res.status(404);
		// const { u_id } = res.locals;
		// const ok = await db.table(TABLE_NAME).where({ id, u_id }).update(data);
		// if (!ok) throw new TargetDataNoneError();
		// return res.send(makeres.putsuccess());
	},
	delete: async ({ params: { id } }, res) => {
		const { u_id } = res.locals;
		const ok = await db.table(TABLE_NAME).where({ id, u_id }).delete();
		if (!ok) throw new TargetDataNoneError();
		return res.send(makeres.deletesuccess());
	},
};
